package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    //申明成员变量
    private ImageView img;
    private Button btn_next;
    private int count = 0;
    //图片存储到数组中
    private int[] imgRes = {R.drawable.leaf1, R.drawable.leaf2, R.drawable.leaf3};
    private Animation mAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //显示图片
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), imgRes[count]);
        img = findViewById(R.id.imageView);
        img.setImageBitmap(bm);
        btn_next = findViewById(R.id.btn_next);
        //设置监听事件
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //计数器换图
                count++;
                if (count == 3) {
                    count = 0;
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = 2;                   //陪缩放图片
                opt.inDither = false;                   //不进行图片抖动处理
                opt.inPreferredConfig = null;           //设置让解码器以最佳方式解码
                Bitmap bm2 = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count], opt);
                //渐变
                mAnimation = AnimationUtils.loadAnimation(MainActivity.this
                        , R.anim.my_alpha);
                img.startAnimation(mAnimation);

                img.setImageBitmap(bm2);
            }
        });
    }
}